%::PROVIDES=(
	name	=> ["START_APP"],
	desc	=> ["Start specified Application"],
	type	=> "request",
	code	=> \&::do_handle_startapp
);

sub ::do_handle_startapp {
my $args=shift;
my ($app,$xml,@m,$dfd,$pid);

	if(!exists($args->{APP})) {
		logwarn("START_APP request missing app argument");
		return "ARG_MISSING";
	}

	$app=$args->{APP};
	if(!exists($appmap->{$app})) {
		logwarn("Specified application ($app) to START not registered");
		return "NOT_REGISTERED";
	}
	#################################################################
	# If the specified application is not running we send back	#
	# a suitable return value.					#
	#################################################################

	if($appmap->{$app}->{STATE} ne "DOWN") {
		logwarn("Specified application ($app) to START is not in DOWN state");
		return "NOT_DOWN";
	}

	################################################################# 
	# Ok - we now see if normal or force option has been specified	#
	# in the application configuration file, defaulting to normal.	#
	################################################################# 

	$xml=XMLin("/etc/cluster/$app/appconf.xml");
	if(!defined($xml)) {
		logwarn("appconf.xml for $app is not valid - not STARTING application!");
		return "INVALID_CONF";
	}

	if(exists($xml->{global}->{takeover})) {
		$failover=$xml->{global}->{takeover};
		if($failover ne "normal" && $failover ne "force") {
			$failover="normal";
			logwarn("Invalid failover type - defaulting to $failover");
		}
	} else {
		$failover="normal";
	}

	#################################################
	# If we have normal then we do not attempt a 	#
	# failover here if any STALE nodes for the 	#
	# local data copy exist.			#
	#################################################

	opendir($dfd,"/etc/cluster/.status/$app");
	@m=grep { /\.$NODE_NAME\.STALE$/ } readdir($dfd);
	closedir($dfd);

	if(scalar(@m) && $failover eq "normal") {
		logwarn("Startup for $app ignored - normal option invalid with locally stale data");
		return "NOT_FORCED";
	}

	#################################################
	# If we get here then and @m is not empty then	#
	# these locally stale flags can be removed, 	#
	# since we're using really-force and would	#
	# discard this information anyway.		#
	#################################################

	foreach $cfile (@m) {
		unlink "/etc/cluster/.status/$app/$cfile";
		logmsg("Removed flag file $cfile for $app on $NODE_NAME");
	}

	logmsg("Now STARTING application $app on node $NODE_NAME!");
	$appmap->{$app}->{STATE}="DOWN";
	$appmap->{$app}->{NODE}="";
	$SIG{CHLD}='DEFAULT';
	$pid=fork();
	if($pid) {
		logmsg("Called clstartapp for $app using PID $pid");
	} else {
		my ($rc,@args);

		if(defined($cld_server)) {
			eval "$cld_server->close_server;";
			$cld_server=undef;
		}

		$SIG{CHLD}=\&waitforit;
		$rc=forknwait("/sbin/cluster/utils/drbd_tool --application $app --action=down");
		select(undef,undef,undef,5);
		@args=("/sbin/cluster/clstartapp","--verbose","--application",$app,"--maxdelay",5,"--force","--logcmd");
		#################################################
		# Direct output to log files for this action.	#
		#################################################

		open(STDOUT,">>/var/log/cluster/$app.start.log");
		open(STDERR,">>/var/log/cluster/$app.start.errlog");
				
		exec { $args[0] } @args; 
	}
	return "OK";
}

1;

